package controllers

import (
	"dev-manage-hub/models"
	beego "github.com/beego/beego/v2/server/web"
)

type DevController struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Date interface{} `json:"date"`
	Msg  string      `json:"msg"`
}

func (c *DevController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	cut := c.GetString("cut")
	user := c.GetString("user")
	address := c.GetString("address")
	liu := models.Dev{
		Id:      id,
		Cut:     cut,
		User:    user,
		Address: address,
	}
	err := models.DB.Create(&liu).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Date: nil,
			Msg:  "添加失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Date: nil,
			Msg:  "添加成功",
		}
		c.ServeJSON()
		return
	}
}
func (c *DevController) List() {
	var liu []models.Dev
	err := models.DB.Find(&liu).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Date: nil,
			Msg:  "查询失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Date: nil,
			Msg:  "查询成功",
		}
		c.ServeJSON()
		return
	}
}
